package com.ruoyi.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.ParkingOrder;
import com.ruoyi.mapper.ParkingOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.MemberInvoiceMapper;
import com.ruoyi.domain.MemberInvoice;
import com.ruoyi.service.IMemberInvoiceService;

import javax.annotation.Resource;

/**
 * 电子发票Service业务层处理
 * 
 * @author dongdong
 * @date 2025-09-22
 */
@Service
public class MemberInvoiceServiceImpl implements IMemberInvoiceService 
{
    @Resource
    private MemberInvoiceMapper memberInvoiceMapper;
    @Resource
    private ParkingOrderMapper parkingOrderMapper;


    /**
     * 查询电子发票
     * 
     * @param id 电子发票主键
     * @return 电子发票
     */
    @Override
    public MemberInvoice selectMemberInvoiceById(Long id)
    {
        return memberInvoiceMapper.selectMemberInvoiceById(id);
    }

    /**
     * 查询电子发票列表
     * 
     * @param memberInvoice 电子发票
     * @return 电子发票
     */
    @Override
    public List<MemberInvoice> selectMemberInvoiceList(MemberInvoice memberInvoice)
    {
        return memberInvoiceMapper.selectMemberInvoiceList(memberInvoice);
    }

    /**
     * 新增电子发票
     * 
     * @param memberInvoice 电子发票
     * @return 结果
     */
    @Override
    public int insertMemberInvoice(MemberInvoice memberInvoice)
    {
        memberInvoice.setCreateTime(DateUtils.getNowDate());
        return memberInvoiceMapper.insertMemberInvoice(memberInvoice);
    }

    /**
     * 修改电子发票
     * 
     * @param memberInvoice 电子发票
     * @return 结果
     */
    @Override
    public int updateMemberInvoice(MemberInvoice memberInvoice)
    {
        memberInvoice.setUpdateTime(DateUtils.getNowDate());
        return memberInvoiceMapper.updateMemberInvoice(memberInvoice);
    }

    /**
     * 批量删除电子发票
     * 
     * @param ids 需要删除的电子发票主键
     * @return 结果
     */
    @Override
    public int deleteMemberInvoiceByIds(Long[] ids)
    {
        return memberInvoiceMapper.deleteMemberInvoiceByIds(ids);
    }

    /**
     * 删除电子发票信息
     * 
     * @param id 电子发票主键
     * @return 结果
     */
    @Override
    public int deleteMemberInvoiceById(Long id)
    {
        return memberInvoiceMapper.deleteMemberInvoiceById(id);
    }

    @Override
    public int issueInvoice(MemberInvoice memberInvoice) {
        memberInvoice.setStatus(2);
        int res = memberInvoiceMapper.updateMemberInvoice(memberInvoice);
        if(res == 1){
            ParkingOrder parkingOrder = new ParkingOrder();
            parkingOrder.setOrderNo(memberInvoice.getOrderNo());
            parkingOrder.setInvoiceStatus(2);
            parkingOrder.setInvoiceId(memberInvoice.getId());
            parkingOrderMapper.updateInvoiceByOrderNo(parkingOrder);
        }
        return res;
    }
}
